package com.prime.tutorial.moviestore.view.admin;

import com.prime.tutorial.moviestore.domain.Groups;
import com.prime.tutorial.moviestore.domain.Users;
import com.prime.tutorial.moviestore.service.admin.AdminService;
import com.prime.tutorial.moviestore.view.auth.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.faces.application.FacesMessage;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Used to edit/remove users
 * Created by IntelliJ IDEA.
 * Date: 25 лют 2010
 * Time: 11:04:42
 * @author iryna.masyuk
 */
@Component("editUsers")
@Scope("session")
public class EditUsers implements Serializable {

    private HtmlDataTable dataTable;

    private AdminService adminService;

    private CurrentUser currentUser;

    //used to store the result of the edit selection
    private Users userSelectedForEdit;


    //groups to populate the dropdown menu
    private List availableGroups;
    private String groupSelected;

    @Autowired
    public EditUsers(AdminService adminService) {
        this.adminService = adminService;
        currentUser=new CurrentUser();
        userSelectedForEdit = null;

        //list of groups to populate the dropdown menu is created
        availableGroups = new ArrayList();
        List<Groups> groups = adminService.getAllGroups();
        for(int i=0; i<groups.size();i++){
            SelectItem option = new SelectItem(groups.get(i).getName(), groups.get(i).getName());
            availableGroups.add(option);
        }
    }

    /**
     * Activated when user clicks on Delete on userList page
     * Deletes the user and displays successfull message
     * @return
     */
    public String delete(){
        //TODO check whether user with active session is not being deleted
        Users ulogged = currentUser.getUser();
        Users urequested = ((Users) dataTable.getRowData());
        if (ulogged.getLogin().equals(urequested.getLogin())){
            //trying to delete current user
            FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Trying to delete current user", "");
            FacesContext.getCurrentInstance().addMessage(null, facesMessage);
            return null;
        } else {
            adminService.deleteUser((Users) dataTable.getRowData());
        }
          return "userList";
    }

    /**
     * Activated when user clicks on Edit on userList page
     * Transfers the user on userEdit page
     * @return
     */
    public String edit(){
        userSelectedForEdit = ((Users) dataTable.getRowData());
        groupSelected =  userSelectedForEdit.getGroup().getName();
        return "edit";
    }

    /**
     * Activated when user clicks on Update Users button
     * Checks validity and passes the update to business level
     * @return
     */
    public String update(){
        //TODO implement

        //updating group if necessary
        if (!userSelectedForEdit.getGroup().getName().equals(groupSelected)){
            userSelectedForEdit.setGroup(adminService.findByName(groupSelected).get(0));
        }
        adminService.updateUser(userSelectedForEdit);
        return "userList";
    }


    //-----------------------------------getters&setters

    public String getGroupSelected() {
        return groupSelected;
    }

    public void setGroupSelected(String groupSelected) {
        this.groupSelected = groupSelected;
    }

    public List getAvailableGroups(){
          return availableGroups;
    }

    public void setAvailableGroups(List availableGroups) {
        this.availableGroups = availableGroups;
    }
    public HtmlDataTable getDataTable() {
        return dataTable;
    }

    public void setDataTable(HtmlDataTable dataTable) {
        this.dataTable = dataTable;
    }

    public Users getUserSelectedForEdit() {
        return userSelectedForEdit;
    }

    public void setUserSelectedForEdit(Users userSelectedForEdit) {
        this.userSelectedForEdit = userSelectedForEdit;
    }
}
